package com.cml.rpc.server;

import com.cml.rpc.publicinterface.MemberService;
import com.cml.rpc.publicinterface.UserService;
import com.cml.rpc.publicpojo.User;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cml
 * @create 2021/7/20 13:06
 * 服务器端实现UserService,MemberService接口，制造一个实现类实现多个接口调用场景
 */
public class UserServiceImpl implements UserService, MemberService {
    private static List<User> userList = new ArrayList<User>();
    // 模拟数据库,创建userList用户集合，后面使用
    static {
        User user1 = new User(123, "xiaomeng", "22");
        User user2 = new User(124, "小鸟猪皮", "23");
        User user3 = new User(125, "我是rpc", "21");
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
    }

    /**
     * 根据id查找用户
     * @param id
     * @return
     */
    @Override
    public User getUserById(Integer id) {
        for (User user:
             userList) {
            Integer userId = user.getId();
            if (userId.equals(id)) {
                System.out.println("获取用户" + id +"成功");
                return user;
            }
        }
        return null;
    }

    @Override
    public Integer insertUser(User user) {
        userList.add(user);
        System.out.println("插入user成功userLise = " + userList);
        return 1;
    }

    @Override
    public User getMemberById(Integer id) {
        return new User(521, "我是会员", "19");
    }
}
